package MathText
{
	import flash.display.Sprite;
	import flash.text.*;
	
	/** This is the top level display class for the MathText package. 
	 * 
	 */ 
	public class WordProblemDisplay extends Sprite
	{
		// vars acting like constants
		public static var BACKGROUND_COLOR:uint = 0x000088, TEXT_COLOR:uint = 0xEEEEFF, SUBTLE_TEXT_COLOR:uint = 0x8888CC;
		// public vars
		public var WIDTH:Number, HEIGHT:Number;
		// private objects
		private var problemTemplateXML:XML, contextXML:XML;
		private var quantityArray:Array;
		private var tField:TextField = null;
		
		// private vars excessible through getter/seter
		private var _wordProblem:WordProblem;
		// getters and setters
		/** Text of the word problem*/
		public function get text():String{return _wordProblem.text;}
		public function get wordProblem():WordProblem {return _wordProblem;}
		public function get contextID():String{return contextXML.@id;}
		
		/** Constructor needs the size of display and the relevant xml files */
		public function WordProblemDisplay(WIDTH:Number, HEIGHT:Number, problemTemplateXML:XML, contextXML:XML, quantityArray:Array, _wordProblem:WordProblem=null)
		{
			this.WIDTH = WIDTH;
			this.HEIGHT = HEIGHT;
			this.graphics.beginFill(BACKGROUND_COLOR);
			this.graphics.drawRect(0,0,WIDTH,HEIGHT);
			this.graphics.endFill();
			this.problemTemplateXML = problemTemplateXML;
			this.contextXML = contextXML;	
			this.quantityArray = quantityArray;
			if (_wordProblem != null)
			{
				this._wordProblem = _wordProblem;
			} else
			{
				this._wordProblem = new WordProblem(problemTemplateXML, contextXML, quantityArray);
			}
			
			drawTextField();
		}
		
		/** Draws the text field, or re-draws it */
		public function drawTextField():void
		{
			if (tField == null)
			{
				tField = new TextField();
				tField.selectable = false;
				tField.wordWrap = true;
				this.addChild(tField);
			}
			tField.defaultTextFormat = new TextFormat("Arial", this.height/4, TEXT_COLOR);
			tField.text = text;
			//tField.wordWrap = true;
			tField.width = (this.width-this.width/8);
			tField.autoSize = "left";
			tField.x = (this.width - tField.width)/2;
			tField.y = (this.height - tField.height)/2;
		}
		/** Displays only the given text, which must be part of the whole string, the rest dissapears into background. */
		public function displaySubstring(subtext:String):Boolean
		{
			var index:int = text.search(subtext);
			if (index >= 0 && subtext.length > 0)
			{
				tField.setTextFormat(new TextFormat("Arial", this.height/4, BACKGROUND_COLOR));
				tField.setTextFormat(new TextFormat("Arial", this.height/4, TEXT_COLOR), index, index+subtext.length);
				return true;	
			} else 
			{
				tField.setTextFormat(new TextFormat("Arial", this.height/4, TEXT_COLOR));
				return false;	
			}
		}
		/** Highlights a portion of the text matching the given subtext */
		public function highlightSubstring(subtext:String):Boolean
		{
			var index:int = text.search(subtext);
			if (index >= 0 && subtext.length > 0)
			{
				tField.setTextFormat(new TextFormat("Arial", this.height/4, SUBTLE_TEXT_COLOR));
				tField.setTextFormat(new TextFormat("Arial", this.height/4, TEXT_COLOR), index, index+subtext.length);
				return true;	
			} else 
			{
				tField.setTextFormat(new TextFormat("Arial", this.height/4, TEXT_COLOR));
				return false;	
			}
		}
	}
}